﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
//using CUrlHelper = JSC.Portal.Web.Helper.UrlHelper;
using JSC.Portal.Services;
using JSC.Core;
using JSC.Portal.Entities;
using JSC.Portal.Web.Models;
using JSC.Portal.Web.Controllers;

namespace JSC.Portal.Web.Helper
{
    public static class MenuExtensions
    {
        private const string JQUERY_SLIDE_MENU = "<div id='myslidemenu' class='jqueryslidemenu'><ul>{0}{1}</ul><br style='clear: left' /></div>";
        private const string SIMPLE_VERTICAL_MENU = "<div id='vertical-navigation'><ul class='vertical-menu'>{0}{1}</ul></div>";
        private const string SIMPLE_HORIZONTAL_MENU = "<div id='menu'><ul id='nav'>{0}{1}</ul><div class='clear'></div></div>";

        #region Horizontal Menu's
        public static string FullHorizontalMenu(this HtmlHelper htmlHelper)
        {
            
            string siteAddress = htmlHelper.GetUrlHelper().GetHostUrl();
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            string language = data.CurrentPage.Language;

            IPortalSiteService svc = FacadeFactory.CreateFacade<IPortalSiteService>();
            PortalSite site = svc.GetPortalSiteByUrl(siteAddress);
            PortalUser user = htmlHelper.ViewContext.CurrentUser();            

            IEnumerable<PortalPage> pages = site.PortalPages.Where(x => x.ShowInNavigation && x.Parent == null && x.ViewAllowed(user) && x.Language == language);

            return string.Format(JQUERY_SLIDE_MENU, GetSubMenuItems(pages, htmlHelper, true, null), GetAdminMenuItem(htmlHelper));
        }

        public static string ParentAndChildrenHorizontalMenu(this HtmlHelper htmlHelper)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            PortalPage page = data.CurrentPage;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();

            IList<PortalPage> list = new List<PortalPage>(page.ChildPages.Where(x => x.ShowInNavigation && x.ViewAllowed(user) && x.Language == page.Language));
            if (page.Parent != null && page.Parent.ViewAllowed(user))
                list.Insert(0, page.Parent);

            return string.Format(JQUERY_SLIDE_MENU, GetSubMenuItems(list, htmlHelper, false, page), GetAdminMenuItem(htmlHelper));
        }

        public static string TwoLevelFlattenedHorizontalMenu(this HtmlHelper htmlHelper)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            PortalPage page = data.CurrentPage;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();

            IList<PortalPage> list = new List<PortalPage>();
            if (page.Parent != null && page.Parent.ViewAllowed(user))
            {
                list.Add(page.Parent);
                foreach (PortalPage pp in page.Parent.ChildPages.Where(x => x.ShowInNavigation && x.ViewAllowed(user) && x.Language == page.Language))
                    list.Add(pp);
            }
            else
            {
                list.Add(page);
                foreach (PortalPage pp in page.ChildPages.Where(x => x.ShowInNavigation && x.ViewAllowed(user)))
                    list.Add(pp);
            }

            return string.Format(JQUERY_SLIDE_MENU, GetSubMenuItems(list, htmlHelper, false, page), GetAdminMenuItem(htmlHelper));
        }
        
        #endregion

        #region Vertical Menu's

        public static string SimpleVerticalParentAndSameLevelMenu(this HtmlHelper htmlHelper)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            PortalPage page = data.CurrentPage;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();
            IList<PortalPage> list = new List<PortalPage>();
            IEnumerable<PortalPage> pages;
            if (page.Parent == null)
            {
                list.Add(page);
                pages = list.Union(page.ChildPages.Where(
                    x => x.ShowInNavigation
                    && x.ViewAllowed(user)
                    && x.Language == page.Language));
            }
            else
            {
                list.Add(page.Parent);
                pages = list.Union(page.Parent.ChildPages.Where(
                    x => x.ShowInNavigation
                    && x.ViewAllowed(user)
                    && x.Language == page.Language));
            }
            string result = string.Format(SIMPLE_VERTICAL_MENU, GetSubMenuItems(pages, htmlHelper, false, page), GetAdminMenuItem(htmlHelper));
            return result;
        }

        public static string SimpleHorizontalParentAndSameLevelMenu(this HtmlHelper htmlHelper, bool recursive)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            PortalPage page = data.CurrentPage;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();
            IList<PortalPage> list = new List<PortalPage>();
            IEnumerable<PortalPage> pages;
            if (page.Parent == null)
            {
                list.Add(page);
                pages = list.Union(page.ChildPages.Where(
                    x => x.ShowInNavigation
                    && x.ViewAllowed(user)
                    && x.Language == page.Language));
            }
            else
            {
                list.Add(page.Parent);
                pages = list.Union(page.Parent.ChildPages.Where(
                    x => x.ShowInNavigation
                    && x.ViewAllowed(user)
                    && x.Language == page.Language));
            }
            string result = string.Format(SIMPLE_HORIZONTAL_MENU, GetSubMenuItems(pages, htmlHelper, recursive, page), GetAdminMenuItem(htmlHelper));
            return result;
        }

        public static string SimpleStaticMenu(this HtmlHelper htmlHelper)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            PortalPage page = data.CurrentPage;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();

            PortalPage root = page.GetRootPage();
            IList<PortalPage> list = new List<PortalPage>();
            IEnumerable<PortalPage> pages;
            list.Add(root);
            pages = list.Union(root.ChildPages.Where(
                    x => x.ShowInNavigation
                    && x.ViewAllowed(user)
                    && x.Language == root.Language));
            string result = string.Format(SIMPLE_HORIZONTAL_MENU, GetSubMenuItems(pages, htmlHelper, true, page), GetAdminMenuItem(htmlHelper));
            return result;
        }

        public static string SimpleVerticalOneLevelMenu(this HtmlHelper htmlHelper)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            PortalPage page = data.CurrentPage;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();
            IList<PortalPage> list = new List<PortalPage>();
            if (page.Parent != null)
                list.Add(page.Parent);
            IEnumerable<PortalPage> pages = list.Union(page.ChildPages.Where(x => x.ShowInNavigation && x.ViewAllowed(user) && x.Language == page.Language));
            string result = string.Format(SIMPLE_VERTICAL_MENU, GetSubMenuItems(pages, htmlHelper, false, page), GetAdminMenuItem(htmlHelper));
            return result;
        }

        #endregion

        public static string Breadcrump(this HtmlHelper htmlHelper)
        {
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            var attr = new { @class = "breadcrump" };
            string result = (data is DesignPageViewData)
                ? htmlHelper.ActionLink(data.CurrentPage.Name, "Design", new { id = data.CurrentPage.Id }, attr).ToHtmlString()
                : htmlHelper.ActionLink(data.CurrentPage.Name, "ShowFriendlyUrl", new { language = data.Language, friendlyurl = data.CurrentPage.FriendlyURL }, attr).ToHtmlString();

            PortalPage parent = data.CurrentPage.Parent;
            while (parent != null)
            {
                if (parent.ShowInNavigation)
                {
                    string link = (data is DesignPageViewData)
                        ? htmlHelper.ActionLink(parent.Name, "Design", new { id = parent.Id }, attr).ToHtmlString()
                        : htmlHelper.ActionLink(parent.Name, "ShowFriendlyUrl", new { language = parent.Language, friendlyurl = parent.FriendlyURL }, attr).ToHtmlString();
                    result = link + " » " + result;
                }
                parent = parent.Parent;
            }
            return result;
        }

        private static string GetAdminMenuItem(HtmlHelper htmlHelper)
        {
            PortalUser user = htmlHelper.ViewContext.CurrentUser();
            TemplatedViewData data = (TemplatedViewData)htmlHelper.ViewData.Model;
            string result = string.Empty;
            if (user != null && (user.IsInRole(Roles.Administrator, Roles.SiteAdministrator)))
            {
                result = string.Format("<li>{0}</li>", htmlHelper.ActionLink("Admin", "Index", ControllerNames.PORTALPAGES_CONTROLLER));
                if (!(data is DesignPageViewData))
                    result += string.Format("<li>{0}</li>", htmlHelper.ActionLink("Design", "Design", ControllerNames.PORTALPAGEDESIGNER_CONTROLLER, new { id = data.CurrentPage.Id }, null));
            }
            return result;
        }

        private static string GetSubMenuItems(IEnumerable<PortalPage> pages, HtmlHelper htmlHelper, bool renderChildren, PortalPage currentPage)
        {
            TemplatedViewData data = (TemplatedViewData) htmlHelper.ViewData.Model;
            string result = string.Empty;
            PortalUser user = htmlHelper.ViewContext.CurrentUser();            

            foreach (PortalPage page in pages)
            {
                var htmlAttr = (currentPage != null && page.Id == currentPage.Id) ? new { @class = "active" } : null;
                result += "<li>";
                result += (data is DesignPageViewData)
                    ? htmlHelper.ActionLink(page.Name, "Design", new { id = page.Id }, htmlAttr)
                    : htmlHelper.ActionLink(page.Name, "ShowFriendlyUrl", new { language = data.Language, friendlyurl = page.FriendlyURL }, htmlAttr);

                if (renderChildren)
                {
                    IEnumerable<PortalPage> childPages = page.ChildPages.Where(x => x.ShowInNavigation && x.ViewAllowed(user) && x.Parent.Parent != null);
                    string subItems = GetSubMenuItems(childPages, htmlHelper, true, currentPage);
                    if (!string.IsNullOrEmpty(subItems))
                    {
                        result += string.Format("<ul class='subnav'>{0}</ul>", subItems);
                    }
                }
                result += "</li>";
            }
            return result;
        }
    }
}
